#include "serivce.h"

Serivce::Serivce(QObject *parent): QObject{parent}
{

}

void Serivce::sendGetRequest()
{

    qDebug()<<"get方法被执行";
    QNetworkRequest request1;
    QNetworkAccessManager nam;
    connect(&nam, &QNetworkAccessManager::finished, this, &Serivce::receiveReplyGet);

    //get与post的请求方式有所不同，get是在接口名后添加 ? 和传输的数据(type)
    request1.setUrl(QUrl("http://localhost:8080/dish/page?hello=666&name=hjl&age=18"));
    request1.setHeader(QNetworkRequest::ContentTypeHeader,QVariant("application/json;charset=utf-8"));
    QString token = "xxxxxxxxxxxxxxxxxxxxxxxx";
    QString token_headerData = token;
    request1.setRawHeader("token",token_headerData.toLocal8Bit());
    QNetworkReply *reply = nam.get(request1);    //get请求头

    //开启事件循环，直到请求完成
    QEventLoop loop;
    connect(reply,&QNetworkReply::finished,&loop,&QEventLoop::quit);
    loop.exec();

}

void Serivce::receiveReplyGet(QNetworkReply *reply)
{
    qDebug()<<"得到get方法的返回值";
    // 获取状态码
    int status_code = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
    // 获取响应数据
    QString reply_data = reply->readAll();
    qDebug()<<reply_data<<status_code;
    resultGet=reply_data;


    //解析json字符串
    QJsonDocument jsonDoc = QJsonDocument::fromJson(reply_data.toUtf8());
    QJsonObject jsonObj = jsonDoc.object();

    QString date = jsonObj["date"].toString();
    int code = jsonObj["code"].toInt();
    QString msg = jsonObj["mes"].toString();

    qDebug()<<reply_data<<status_code<<date<<msg<<code;


}



void Serivce::sendPostRequest()
{
    qDebug()<<"post方法被执行";

    QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
    //封装文本参数
    QHttpPart textPart;
    textPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=text"));
    textPart.setBody("text");


    //封装文件参数
    QHttpPart filePart;
    filePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=file; filename=output.wav"));
    QString filename="F:\\output.wav";
    QFile file(filename);
    QByteArray bytes;

    if (!file.exists()) {
        qDebug() << "文件不存在";
        return;
    }

    if (file.open(QFile::ReadOnly)) {
        bytes = file.readAll();
        qDebug() << "状态：读取文件成功";
    }
    file.close();
    filePart.setBody(bytes);
    qDebug()<<bytes.size();


    //将封装到的参数加入到这个对象中
    multiPart->append(textPart);
    multiPart->append(filePart);



    QUrl url("http://localhost:8080/common/upload?age=18&hobby=football");
    QNetworkRequest request(url);
    request.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("multipart/form-data; boundary="+multiPart->boundary()));

    QNetworkAccessManager manager;

    connect(&manager, &QNetworkAccessManager::finished, this, &Serivce::receiveReplyPost);
    QNetworkReply *reply = manager.post(request, multiPart);



    //开启事件循环，直到请求完成
    QEventLoop loop;
    connect(reply,&QNetworkReply::finished,&loop,&QEventLoop::quit);
    loop.exec();


}

void Serivce::receiveReplyPost(QNetworkReply *reply)
{
    qDebug()<<"post方法返回";
    // 获取状态码
    int status_code = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
    // 获取响应数据
    QString reply_data = reply->readAll();
    resultPost=reply_data;


    //解析json字符串
    QJsonDocument jsonDoc = QJsonDocument::fromJson(reply_data.toUtf8());
    QJsonObject jsonObj = jsonDoc.object();

    QString date = jsonObj["data"].toString();
    int code = jsonObj["code"].toInt();
    QString msg = jsonObj["msg"].toString();

    qDebug()<<reply_data<<status_code<<date<<msg<<code;

}

